package com.magnificent.panda.model.generator.log;

import com.silverhaze.commons.helpers.CommonUIHelper;
import com.silverhaze.commons.helpers.UI;
import com.magnificent.panda.correlation.IntervalCorrelationFunction;
import com.magnificent.panda.correlation.ProhorovCorrelationFunction;
import com.magnificent.panda.model.entity.FlowEvent;
import com.magnificent.panda.utils.JFreeChartHelper;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * User: Babanin
 * Date: 07.10.2010
 * Time: 16:10:28
 */
public class BuildFlow {
    private static final String FLOW_1 = "output\\0_start.txt";
    private static final String FLOW_2 = "output\\patents.txt";

    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("a".contains(" "));
//        new CorrelationSettingsDialog(readFlow(FLOW_1), readFlow(FLOW_2)).setVisible(true);

    }

    private static List<Integer> readFlow(String flow2) throws FileNotFoundException {
        List<Integer> result = new ArrayList<Integer>();
        Scanner scanner = new Scanner(new File(flow2));

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();

            if (!"".equals(line)) {
                result.add(Integer.valueOf(line));
            }
        }

        scanner.close();
        return result;
    }

    public static class CorrelationSettingsDialog extends JFrame {

        private List<Integer> flowOne;
        private List<Integer> flowTwo;

        private JButton countButton;
        private JSpinner discreteIntervalSpinner;
        private JSpinner countCorrelationSpinner;
        private JComboBox correlationFunctionComboBox;

        private JFreeChart correlationChart;
        private ChartPanel correlationChartPanel;

        private IntervalCorrelationFunction intervalCorrelationFunction;

        public CorrelationSettingsDialog(List<Integer> flowOne, List<Integer> flowTwo) {
            this.flowOne = flowOne;
            this.flowTwo = flowTwo;

            initializeControls();
            initializeView();
        }

        private void initializeView() {
            setTitle("пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ");
            setLayout(new BorderLayout());
            getRootPane().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

            add(CommonUIHelper.createBorderLayoutPanel(
                    new JComponent[]{
                            CommonUIHelper.createHorizontalBox(
                                    "пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ: ", discreteIntervalSpinner, 10,
                                    "пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ: ", countCorrelationSpinner
                            ),
                            UI.hBox(countButton)
                    },
                    new Object[]{
                            BorderLayout.LINE_START,
                            BorderLayout.LINE_END
                    }
            ), BorderLayout.NORTH);


            add(CommonUIHelper.packInShiftedPanel(correlationChartPanel, 5, 5, 0, 0), BorderLayout.CENTER);

            add(CommonUIHelper.packInPanel(new JButton(new AbstractAction("пїЅпїЅпїЅпїЅпїЅпїЅпїЅ") {
                public void actionPerformed(ActionEvent e) {
                    CorrelationSettingsDialog.this.dispose();
                }
            }), BorderLayout.LINE_END), BorderLayout.SOUTH);

            pack();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        private void initializeControls() {
            intervalCorrelationFunction = new ProhorovCorrelationFunction();

            countButton = new JButton(new AbstractAction("пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ") {
                public void actionPerformed(ActionEvent e) {
                    FlowEvent[] masterValues = convertToFlowEvents(flowOne);
                    FlowEvent[] secondFlowDoubles = convertToFlowEvents(flowTwo);

                    double[] correlation = intervalCorrelationFunction.calculateCorrelation(masterValues, secondFlowDoubles,
                            (Integer) countCorrelationSpinner.getValue(), (Integer) discreteIntervalSpinner.getValue());

                    double[] correlationMixed = intervalCorrelationFunction.calculateCorrelation(masterValues, secondFlowDoubles,
                            (Integer) countCorrelationSpinner.getValue(), (Integer) discreteIntervalSpinner.getValue());

                    updateGraph(correlation, correlationMixed);
                }
            });
            discreteIntervalSpinner = new JSpinner(new SpinnerNumberModel(5, 1, 100, 1));
            countCorrelationSpinner = new JSpinner(new SpinnerNumberModel(5, 1, 1000, 1));

            correlationChart = JFreeChartHelper.newCorrelationChart(JFreeChartHelper.createFlowDataset(new ArrayList<FlowEvent>()), "", "", "");

            correlationChartPanel = new ChartPanel(correlationChart);
            correlationChartPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            correlationFunctionComboBox = new JComboBox(new IntervalCorrelationFunction[]{
                    new ProhorovCorrelationFunction(),
            });
        }

        private FlowEvent[] convertToFlowEvents(List<Integer> flowOne) {
            FlowEvent[] result = new FlowEvent[flowOne.size()];

            for (int i = 0; i < flowOne.size(); i++) {
                result[i] = new FlowEvent(i, flowOne.get(i));

            }

            return result;
        }

        private void updateGraph(double[] correlation, double[] correlationMixed) {
            XYDataset dataset = JFreeChartHelper.createDataset(correlation, correlationMixed);

            correlationChart.getXYPlot().setDataset(0, dataset);
        }
    }
}

  /* converted to utf8 */